package redblacktree;

import map.Map;

import java.util.Objects;

/**
 * 红黑树 (B-Tree)
 *
 * @author gy
 * @date 2019/6/1 7:08
 */
public class RBTree<K extends Comparable<K>, V> implements Map<K, V> {


    private Node root;
    private int size;

    public RBTree(Node node, int size) {
        this.root = null;
        this.size = 0;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    private boolean isRed(Node node) {
        if (Objects.isNull(node)) {
            return BLACK;
        }
        return node.color;
    }


    private Node leftRotate(Node node){

        Node x = node.right;

        node.right = x.left;
        x.left = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }


    private Node rightRotate(Node node){

        Node x = node.left;

        node.left = x.right;
        x.right = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }


    /**
     * 翻转颜色
     * @param node
     */
    private void filpColors(Node node){

        node.color = RED;
        node.left.color = BLACK;
        node.right.color = BLACK;

    }


    @Override
    public void add(K key, V value) {

        root = add(root, key, value);
        //keep the root node is always black
        root.color = BLACK;
    }

    private Node add(Node node, K key, V value) {

        if (Objects.isNull(node)) {
            size++;
            // 默认插入红色节点
            return new Node(key, value);
        }

        if (key.compareTo(node.key) < 0) {
            node.left = add(node.left, key, value);
        } else if (key.compareTo(node.key) > 0) {
            node.right = add(node.right, key, value);
        } else {
            node.value = value;
        }


        if(isRed(node.left) && !isRed(node.right)){
            node = leftRotate(node);
        }

        if(isRed(node.left) && isRed(node.left.left)){
            node = rightRotate(node);
        }

        if(isRed(node.left) && isRed(node.right)){
            filpColors(node);
        }



        return node;
    }

    private Node getNode(Node node, K key) {

        if (Objects.isNull(node)) {
            return null;
        }

        if (key.compareTo(node.key) == 0) {
            return node;
        } else if (key.compareTo(node.key) < 0) {
            return getNode(node.left, key);
        } else {
            return getNode(node.right, key);
        }

    }

    @Override
    public V remove(K key) {

        Node node = getNode(root, key);
        if (Objects.nonNull(node)) {
            root = remove(root, key);
            return node.value;
        }
        return null;
    }

    private Node remove(Node node, K key) {

        if (Objects.isNull(node)) {
            return null;
        }

        if (key.compareTo(node.key) < 0) {
            node.left = remove(node.left, key);
            return node;
        } else if (key.compareTo(node.key) > 0) {
            node.right = remove(node.right, key);
            return node;
        } else {

            if (Objects.isNull(node.left)) {

                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;

            }

            if (Objects.isNull(node.right)) {

                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;

            }

            Node successor = minimum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;

            node.left = node.right = null;

            return successor;
        }

    }

    private Node minimum(Node node) {

        if (Objects.isNull(node.left)) {
            return node;
        }

        return minimum(node.left);
    }


    private Node removeMin(Node node) {

        if (Objects.isNull(node.left)) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    @Override
    public boolean contains(K key) {
        return Objects.nonNull(getNode(root, key));
    }

    @Override
    public V get(K key) {
        Node node = getNode(root, key);
        return Objects.isNull(node) ? null : node.value;
    }

    @Override
    public void set(K key, V newValue) {
        Node node = getNode(root, key);
        if (Objects.isNull(node)) {
            throw new IllegalArgumentException(key + "doesn't exist!");
        }
        node.value = newValue;
    }

    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node {

        public K key;
        public V value;
        public Node left, right;
        public boolean color;

        public Node(K key, V value) {

            this.key = key;
            this.value = value;

            left = null;
            right = null;

            color = RED;
        }
    }
}
